package org.dut.aoptest.Aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Slf4j
@Aspect
//@Component
@Order(1)
public class TimeAspect {
    @Pointcut("execution(* org.dut.aoptest.Controller.*.*(..))")
    public void pointcut() {}

    @Around("pointcut()")
    public Object recordTime(ProceedingJoinPoint joinPoint) throws Throwable {
        long begin = System.currentTimeMillis();
        log.info("Around执行前");
        Object result = joinPoint.proceed();
        log.info("Around执行后");
        long end = System.currentTimeMillis();
        long time = end - begin;
        log.info("Aspect time: {} ms", time);
        return result;
    }
    @AfterThrowing("pointcut()")
    public void afterThrowing(){
        log.info("执行AfterThrowing方法");
    }
}
